import { SET_USER_LIST,SET_GROUP_LIST,SET_TAG_LIST,SET_PROFILE,SET_GROUP_DETAIL,REMOVE_GROUPS,SET_TAG_DETAIL,REMOVE_TAGS,REMOVE_USERS} from '../actions/types';
var array = require('lodash/array');
const initialState = {
  profile: {},
  users: [],
  groups: [],
  tags: [],
  group:{},
  tag:{},
};

export default (state = initialState, action = {}) => {
  switch(action.type) {
    case SET_USER_LIST:
      return Object.assign({}, state, {
        users: action.users
      });
    case REMOVE_USERS:
      var new_users = state.users.filter((value)=>{
         return array.indexOf(action.users, value.id);
      });
      return  Object.assign({}, state, {
            users: new_users
        });
    case SET_GROUP_LIST:
      return Object.assign({}, state, {
        groups:action.groups
       });
    case SET_TAG_LIST:
      return  Object.assign({}, state, {
        tags:action.tags
      });
    case SET_PROFILE:
      return  Object.assign({}, state, {
          profile:action.profile
      });
    case SET_GROUP_DETAIL:
      return  Object.assign({}, state, {
            group:action.group
        });
    case REMOVE_GROUPS:
      var new_groups = state.groups.filter((value)=>{
         return array.indexOf(action.groups, value.id);
      });
      return  Object.assign({}, state, {
            groups:new_groups
        });
    case SET_TAG_DETAIL:
      return  Object.assign({}, state, {
            tag:action.tag
        });
    case REMOVE_TAGS:
      var new_tags = state.groups.filter((value)=>{
         return array.indexOf(action.tags, value.id);
      });
      return  Object.assign({}, state, {
            tags:new_tags
        });

    default: return state;
  }
}